<script setup lang="ts">
import { ref, inject } from "vue";
import { updateUser } from "@/service/api";
import addFile from "../../components/addFile.vue";
import { ElMessage } from "element-plus";
import cloneDeep from 'lodash/cloneDeep';


const visible = ref(false)

const defaultFrom = {
    id: 0,
    username: "",
    password: "",
    nickname: "",
    headUrl: 0,
    gender: 0,
    note: "",
    email: "",
    mobile: 0,
    idCard: "",
    createDate: "",
    status: 0,
    superAdmin: 0,
    delFlag: 0
}
const url = ref()
// 表单数据
const getUserList = inject<any>('getUserList')
const fromModel = ref({ ...defaultFrom });
const open = (row: any) => {
    visible.value = true
    fromModel.value = { ...defaultFrom, ...cloneDeep(row) };
}

const editUser = async () => {
    const { id, username, nickname, headUrl, gender, note, email, mobile, idCard, createDate, status, superAdmin, delFlag } = fromModel.value
    const Mpassword = ""
    if (fromModel.value.password === null) {
        fromModel.value.password = Mpassword
    }
    await updateUser({ id, username, password: Mpassword, nickname, headUrl: url.value.coverId, gender, note, email, mobile, idCard, createDate, status, superAdmin, delFlag }).then(res => {
        if (res.data.code === 200) {
            ElMessage.success('修改成功！！！')
            getUserList()
            visible.value = false
            console.log(res.data);
        } else {
            ElMessage.error('修改失败！！！')
        }
    }).catch(err => {
        console.log(err);
    })
}
defineExpose({
    open
})
</script>


<template>
    <el-dialog v-model="visible" :close-on-click-modal="false" :close-on-press-escape="false">
        <el-form ref="dataFormRef" label-width="120px">
            <el-form-item label="头像" prop="headerUrl">
                <addFile ref="url"></addFile>
            </el-form-item>
            <el-form-item label="昵称" prop="nickname">
                <el-input placeholder="昵称" v-model="fromModel.nickname"></el-input>
            </el-form-item>
            <el-form-item label="个性签名" prop="note">
                <el-input placeholder="个性签名" v-model="fromModel.note"></el-input>
            </el-form-item>
        </el-form>
        <template #footer>
            <span>
                <el-button @click="visible = false" style="margin-right: 10px;">取消</el-button>
                <el-button type="primary" @click="editUser">确定</el-button>
            </span>
        </template>
    </el-dialog>
</template>

<style scoped lang="less"></style>